// package-market/pages/lottery-edit-timerange/index.js
const app = getApp();
import { navBack, tipToast } from '../../../common/env';

Page({
    data: {
        activityInfo: {},
        startTimePickerPopup: {
            show: false,
        },
        endTimePickerPopup: {
            show: false,
        },
        timeRange: {},
    },
    onLoad(options) {
        app.event.addListener('sendLotteryActivityInfo', this._handleSendActivity.bind(this));
    },
    onUnload() {
        app.event.removeListener('sendLotteryActivityInfo');
    },
    _handleSendActivity(activityInfo) {
        const { timeRanges } = activityInfo;
        this.setData({
            activityInfo,
            timeRange: timeRanges[0],
        });
    },
    handleShowStartTimePickerPopup() {
        this.setData({
            'startTimePickerPopup.show': true,
        });
    },
    handleShowEndTimePickerPopup() {
        this.setData({
            'endTimePickerPopup.show': true,
        });
    },
    handleChangeStartTime(e) {
        this.setData({
            'timeRange.beginAt': e.detail,
        });
    },
    handleChangeEndTime(e) {
        this.setData({
            'timeRange.endAt': e.detail,
        });
    },
    handleSave(e) {
        const { activityInfo, timeRange } = this.data;
        const { beginAt, endAt } = timeRange;
        if ((beginAt && !endAt) || (!beginAt && endAt)) {
            tipToast('请选择完整的生效时段');
            return;
        } else if ((beginAt.replace(':', '') - 0) >= (endAt.replace(':', '') - 0)) {
            tipToast('开始时段需小于结束时段');
            return;
        } else {
            activityInfo.timeRanges = [timeRange];
        }
        app.event.trigger('getLotteryActivityInfo', activityInfo);
        navBack();
    },
})
